package com.tech.connect.zhaofuwu;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.tech.connect.R;
import com.tech.connect.activity.BaseActivity;
import com.tech.connect.api.BaseEntityOb;
import com.tech.connect.api.FuWuHttp;
import com.tech.connect.api.HttpUtils;
import com.tech.connect.api.UserInfo;
import com.tech.connect.model.CallBack;
import com.tech.connect.model.ItemFuWu;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ZhaoFuWuFuWuViewActivity extends BaseActivity {

    private ImageView ivReturn;
    private ImageView ivDelete;
    private int totalCount;
    private int currentItem = 0;
    private List<ItemFuWu> allList = new ArrayList<>();
    private FuWuView view;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhaofuwu_fuwu_view);
        Intent intent = getIntent();
        Serializable dataList = intent.getSerializableExtra("dataList");
        if (dataList != null && dataList instanceof ArrayList) {
            allList.addAll((List<ItemFuWu>) dataList);
        }
        if (allList.isEmpty()) {
            showToast("暂无服务数据");
            destroyActivity();
            return;
        }
        int index = intent.getIntExtra("index", 0);
        if (index < 0 || index >= allList.size()) {
            index = 0;
        }
        currentItem = 0;
        totalCount = allList.size();
        UserInfo user = allList.get(currentItem).user;
        String titleString = getTitleString(user);
        getHeadBar().setTitle(titleString);
        getHeadBar().setRightIcon(R.drawable.icon_share, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        initView();
    }

    private String getTitleString(UserInfo userInfo) {
        if (userInfo != null) {
            return userInfo.nickName;
        }
        return "";
    }

    private void initView() {
        view = findViewById(R.id.fuWuView);
        ivReturn = findViewById(R.id.ivReturn);
        ivDelete = findViewById(R.id.ivDelete);
        Button btChat = findViewById(R.id.btChat);
        Button btFollow = findViewById(R.id.btFollow);
        ivReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentItem <= 0) {
                    showToast("前面没有了");
                    return;
                }
                currentItem--;
                updateView();
            }
        });
        ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentItem >= totalCount - 1) {
                    showToast("没有更多了");
                    return;
                }
                currentItem++;
                updateView();
            }
        });
        btChat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        btFollow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ItemFuWu item = allList.get(currentItem);
                follow(item);

            }
        });
        updateView();
    }

    private void updateView() {
        final ItemFuWu item = allList.get(currentItem);
        showLog(item);
        getHeadBar().setTitle(getTitleString(item.user));
        view.update(item, new CallBack() {
            @Override
            public void onStart() {
                showDialog();
            }

            @Override
            public void onFinish() {
                hideDialog();
                view(item.id);
            }
        });
        view.setYueDanClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, FuWuYueDanActivity.class);
                intent.putExtra("data", item);
                jump2Activity(intent, 996);
            }
        });
        view.setZanClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                praise(item);
            }
        });
    }

    private void praise(final ItemFuWu item) {
        Map<String, Object> map = HttpUtils.getMap();
        map.put("id", item.id);
        map.put("type", 40);
        HttpUtils.praise(map, new BaseEntityOb<Integer>() {
            @Override
            public void onStart() {
                super.onStart();
                showDialog();
            }

            @Override
            public void onDataDeal(boolean isSuccess, Integer data, String msg) {
                hideDialog();
                showToast(msg);
                item.likeCount = data;
                view.tvPraise.setText(String.valueOf(item.likeCount));
            }
        });
    }

    private void follow(ItemFuWu item) {
        Map<String, Object> map = HttpUtils.getMap();
        map.put("type", item.id);
        map.put("typeId", 2);
        HttpUtils.follow(map, new BaseEntityOb<Map<String, String>>() {
            @Override
            public void onStart() {
                super.onStart();
                showDialog();
            }

            @Override
            public void onDataDeal(boolean isSuccess, Map<String, String> data, String msg) {
                hideDialog();
                showToast(msg);
            }
        });
    }


    private void view(long id) {
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        FuWuHttp.view(map, new BaseEntityOb<Map<String, String>>() {
            @Override
            public void onDataDeal(boolean isSuccess, Map<String, String> data, String msg) {

            }
        });
    }


}